package com.woniu.config;

import com.woniu.util.ConstantUtil;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;


@Configuration
public class RabbitMQConfig {
    // 15分钟后检查订单是否支付 延迟队列
    // 一个队列
    @Bean
    public Queue checkOrderStatus(){
        return new Queue(ConstantUtil.RABBIT_QUEUE_CHECK_ORDER_STATUS);
    }
    // 一个延迟交换机
    @Bean
    public CustomExchange customExchange(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("x-delayed-type","topic");
        return new CustomExchange(
                ConstantUtil.RABBIT_CUSTOM_EXCHANGE,
                "x-delayed-message",true,
                false,map
        );
    }
    // 绑定交换机 和 队列
    @Bean
    public Binding checkOrderStatusToExchange(){
        return BindingBuilder.bind(checkOrderStatus()).to(customExchange())
                .with(ConstantUtil.ROUTING_KEY_CHECK_ORDER_STATUS).noargs();
    }


    // 异步生成订单 topic
    @Bean
    public Queue orderQueue(){
        return new Queue(ConstantUtil.RABBIT_QUEUE_ORDER);
    }
    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange(ConstantUtil.RABBIT_EXCHANGE_TOPIC);
    }
    @Bean
    public Binding orderQueueToExchange(){
        return BindingBuilder.bind(orderQueue()).to(topicExchange()).with(ConstantUtil.ROUTING_KEY_ORDER);
    }
    //产品延迟队列 1天检查
    @Bean
    public Queue checkProductStatus(){
        return new Queue(ConstantUtil.RABBIT_QUEUE);
    }

    @Bean
    public CustomExchange productExchange(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("x-delayed-type","topic");
        return new CustomExchange(
                ConstantUtil.RABBIT_EXCHANGE,
                "x-delayed-message",true,
                false,map
        );
    }
    @Bean
    public Binding checkProductStatusToExchange(){
        return BindingBuilder.bind(checkProductStatus()).to(productExchange())
                .with("product").noargs();
    }
}
